package com.atguigu.dao.impl;

import com.atguigu.dao.MovieDao;
import com.atguigu.pojo.Movie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author 琉
 * @create 2021-09-06 18:10
 */
@Repository
public class MovieDaoImpl implements MovieDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public List<Movie> getAll() {
        String sql = "select movie_id movieId, movie_name movieName,movie_price moviePrice from t_movie";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Movie.class));
    }


    @Override
    public Movie getMovieById(String movieId) {
        String sql ="select movie_id movieId,movie_name movieName,movie_price moviePrice from t_movie where movie_id =?";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(Movie.class),movieId);
    }


    //添加电影名
    @Override
    public void saveMovie(Movie movie) {
        String sql = "insert into t_movie(movie_name,movie_price) values(?,?)";
        jdbcTemplate.update(sql,movie.getMovieName(),movie.getMoviePrice());
    }

    //修改电影名
    @Override
    public void updateMovie(Movie movie) {
        String sql = "update t_movie set movie_name=?,movie_price=? where movie_id=?";
        jdbcTemplate.update(sql,movie.getMovieName(),movie.getMoviePrice(),movie.getMovieId());
    }

    //根据ID删除电影
    @Override
    public void removeMovieById(String movieId) {
        String sql = "delete from t_movie where movie_id = ?";
        jdbcTemplate.update(sql,movieId);
    }

}
